package com.shaoshaohuo.app.view;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.net.HttpConfig;
import com.shaoshaohuo.app.utils.sharesdk.ShareContent;
import com.shaoshaohuo.app.utils.sharesdk.ShareFactory;

import cn.sharesdk.douban.Douban;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by Administrator on 2017/6/8.
 */

public class ShareDialog extends Dialog {

    private int[] imgeres = new int[]{R.mipmap.wechat, R.mipmap.quan, R.mipmap.qq, R.mipmap.weibo, R.mipmap.qqkongjian, R.mipmap.douban};
    private String[] strs = new String[]{"微信", "朋友圈", "QQ", "微博", "QQ空间", "豆瓣"};
    private LayoutInflater from;

    public ShareDialog(@NonNull Context context) {
        super(context, R.style.dialog);
        init();
    }

    private Platform.ShareParams shareParams;

    public void setshareParams(Platform.ShareParams params) {
        this.shareParams = params;
    }

    private void init() {
        from = LayoutInflater.from(getContext());
        View inflate = from.inflate(R.layout.sharedialoglayout, null);
        setContentView(inflate);
        GridView sharedialog_grid = (GridView) findViewById(R.id.sharedialog_grid);
        Button sharedialog_btncanle = (Button) findViewById(R.id.sharedialog_btncanle);

        //设置动画效果
        getWindow().setWindowAnimations(R.style.Anim_Dialog_Bottom);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.BOTTOM;
        attributes.width = getContext().getResources().getDisplayMetrics().widthPixels;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        attributes.dimAmount = 0.6f;
        window.setAttributes(attributes);
        sharedialog_btncanle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        Myadapter myadapter = new Myadapter();
        sharedialog_grid.setAdapter(myadapter);
        sharedialog_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (shareParams == null) {
                    shareParams = new Platform.ShareParams();
                    shareParams.setTitle(ShareContent.SHARE_APP_CONTENT);
                    shareParams.setTitleUrl(HttpConfig.WebPage.SHARE);
                    shareParams.setText(ShareContent.SHARE_APP_CONTENT);
                    shareParams.setImageUrl(HttpConfig.WebPage.SHARE_IMAGE_PATH);
                    shareParams.setUrl(HttpConfig.WebPage.SHARE);
                }

                switch (position) {
                    case 0:
                        shareParams.setVenueName(Wechat.NAME);
                        break;
                    case 1:
                        shareParams.setVenueName(WechatMoments.NAME);
                        break;
                    case 2:
                        shareParams.setVenueName(QQ.NAME);
                        break;
                    case 3:
                        shareParams.setVenueName(SinaWeibo.NAME);
                        break;
                    case 4:
                        shareParams.setVenueName(QZone.NAME);
                        break;
                    case 5:
//                        params.setVenueName(Douban.NAME);
                        break;

                }

                ShareFactory.showShare(getContext(), shareParams, ShareDialog.this);
            }
        });

    }

    class Myadapter extends BaseAdapter {

        @Override
        public int getCount() {
            return imgeres.length;
        }

        @Override
        public Object getItem(int position) {
            return imgeres[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Myholder myholder;
            if (convertView == null) {
                convertView = from.inflate(R.layout.sharegridlayout, null);
                myholder = new Myholder();
                convertView.setTag(myholder);
                myholder.imageView = (ImageView) convertView.findViewById(R.id.share_griditem_iv);
                myholder.textView = (TextView) convertView.findViewById(R.id.share_griditem_tv);
            } else {
                myholder = (Myholder) convertView.getTag();
            }
            myholder.imageView.setImageResource(imgeres[position]);
            myholder.textView.setText(strs[position]);
            return convertView;
        }

        class Myholder {
            TextView textView;
            ImageView imageView;
        }
    }

}
